{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import chimera\n",
    "import chemview\n",
    "import numpy as np\n",
    "chemview.enable_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fetching 1RFO from web site www.rcsb.org\n",
      "0 Kbytes received\n",
      "72 Kbytes received\n",
      "176 Kbytes received\n",
      "312 Kbytes received\n",
      "456 Kbytes received\n",
      "600 Kbytes received\n",
      "744 Kbytes received\n",
      "880 Kbytes received\n",
      "1024 Kbytes received\n",
      "Fetch 1RFO: finished\n",
      "Done fetching 1RFO; verifying...\n",
      "Opening 1RFO...\n",
      "#0.8, chain A: whisker antigen control protein\n",
      "\n",
      "#0.8, chain B: whisker antigen control protein\n",
      "\n",
      "#0.8, chain C: whisker antigen control protein\n",
      "\n",
      "#0.9, chain A: whisker antigen control protein\n",
      "\n",
      "#0.9, chain B: whisker antigen control protein\n",
      "\n",
      "#0.9, chain C: whisker antigen control protein\n",
      "\n",
      "#0.10, chain A: whisker antigen control protein\n",
      "\n",
      "#0.10, chain B: whisker antigen control protein\n",
      "\n",
      "#0.10, chain C: whisker antigen control protein\n",
      "\n",
      "#0.2, chain A: whisker antigen control protein\n",
      "\n",
      "#0.2, chain B: whisker antigen control protein\n",
      "\n",
      "#0.2, chain C: whisker antigen control protein\n",
      "\n",
      "#0.3, chain A: whisker antigen control protein\n",
      "\n",
      "#0.3, chain B: whisker antigen control protein\n",
      "\n",
      "#0.3, chain C: whisker antigen control protein\n",
      "\n",
      "#0.1, chain A: whisker antigen control protein\n",
      "\n",
      "#0.1, chain B: whisker antigen control protein\n",
      "\n",
      "#0.1, chain C: whisker antigen control protein\n",
      "\n",
      "#0.4, chain A: whisker antigen control protein\n",
      "\n",
      "#0.4, chain B: whisker antigen control protein\n",
      "\n",
      "#0.4, chain C: whisker antigen control protein\n",
      "\n",
      "#0.5, chain A: whisker antigen control protein\n",
      "\n",
      "#0.5, chain B: whisker antigen control protein\n",
      "\n",
      "#0.5, chain C: whisker antigen control protein\n",
      "\n",
      "#0.6, chain A: whisker antigen control protein\n",
      "\n",
      "#0.6, chain B: whisker antigen control protein\n",
      "\n",
      "#0.6, chain C: whisker antigen control protein\n",
      "\n",
      "#0.7, chain A: whisker antigen control protein\n",
      "\n",
      "#0.7, chain B: whisker antigen control protein\n",
      "\n",
      "#0.7, chain C: whisker antigen control protein\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/home/jrgp/.local/UCSF-Chimera64-alpha/share/chimera/triggerSet.py\", line 83, in invoke\n",
      "    self._funcData, triggerData)\n",
      "  File \"/home/jrgp/.local/UCSF-Chimera64-alpha/share/chimera/__init__.py\", line 2605, in categorizeSurface\n",
      "    processNewMolecules(prefModels)\n",
      "  File \"/home/jrgp/.local/UCSF-Chimera64-alpha/share/chimera/__init__.py\", line 2479, in processNewMolecules\n",
      "    hlColor = viewer.highlightColor\n",
      "AttributeError: '_chimera.NoGuiViewer' object has no attribute 'highlightColor'\n",
      "\n",
      "10 models opened\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Error processing trigger \"Bond\":\n",
      "AttributeError: '_chimera.NoGuiViewer' object has no attribute 'highlightColor'\n",
      "\n",
      "  File \"/home/jrgp/.local/UCSF-Chimera64-alpha/share/chimera/__init__.py\", line 2479, in processNewMolecules\n",
      "    hlColor = viewer.highlightColor\n",
      "\n",
      "See reply log for Python traceback.\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "chimera.runCommand(\"open 1RFO\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "m = chimera.openModels.list()[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "atoms = sorted(m.atoms, key=lambda a: a.serialNumber)\n",
    "atom_map = {a: i for (i,a) in enumerate(atoms)}\n",
    "coords = np.array([a.coord() for a in atoms])\n",
    "types = tuple(a.element.name for a in atoms)\n",
    "names = tuple(a.name for a in atoms)\n",
    "bonds = tuple(tuple(atom_map[a] for a in b.atoms) for b in m.bonds)\n",
    "residues = sorted(m.residues, key=lambda r: r.id.position)\n",
    "residue_atoms = tuple(tuple(atom_map[a] for a in r.atoms) for r in residues)\n",
    "residue_types = tuple(r.type for r in residues)\n",
    "ss = {(True, False): \"H\", (False, True): \"E\", (False, False): \"C\"}\n",
    "secondary_structure = tuple(ss[(r.isHelix, r.isSheet)] for r in residues)\n",
    "topology = {'atom_types': types, \n",
    "            'atom_names': names, \n",
    "            'bonds':bonds,\n",
    "            'residue_indices': residue_atoms,\n",
    "            'residue_types': residue_types,\n",
    "            'secondary_structure': secondary_structure}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "v = chemview.MolecularViewer(coords, topology)\n",
    "v.lines()\n",
    "v.line_ribbon()\n",
    "v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import time\n",
    "for i in [1]*10+[-1]*10+[-1]*10+[1]*10:\n",
    "    v.coordinates += i\n",
    "    time.sleep(0.1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
